package com.mylove.textprogressbar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author myLove
 * @time 2018/1/3 11:18
 * @e-mail love@yanyi.red
 * @overview
 */

public class SharedUtil {
    @SuppressLint("StaticFieldLeak")
    private static SharedUtil instance;
    private SharedPreferences sha;
    private SharedPreferences.Editor oEditor;
    private String name;
    private Context mContext;

    private SharedUtil(String name, Context context) {
        this.name = name;
        this.mContext = context;
    }

    public static SharedUtil getInstance(String name, Context context) {
        if (instance == null) {
            synchronized (SharedUtil.class) {
                if (instance == null) {
                    instance = new SharedUtil(name, context);

                }
            }
        }
        return instance;
    }

    /**
     * 数据保存本地
     *
     * @param key    保存的key值
     * @param values 保存的values
     */
    public void saveShaBoolean(String key, boolean values) {
        sha = mContext.getSharedPreferences(name, Context.MODE_PRIVATE);
        oEditor = sha.edit();
        oEditor.putBoolean(key, values);
        oEditor.apply();
    }

    /**
     * 从本地获取数据
     *
     * @param key 保存的key值
     * @return 值
     */
    public boolean getShaBoolean(String key, boolean defaultValue) {
        sha = mContext.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sha.getBoolean(key, defaultValue);
    }
}
